---
title: 'GitHub Sync'
tags: 'github'
description: ''
---

Sync 2 repositories on push.

## Generate Personal Access Token

The GitHub Account must have **access to both repositories.**

1. Visit [Token Settings](https://github.com/settings/tokens)
2. Generate a new token

   ![<CloudinaryImg
     mdx
     publicId='theodorusclarence/shorts/github/github-sync/github-sync-1'
     alt='github-sync-1'
     width={2232}
     height={1088}
   />

3. Fill as below, this will be used to force push changes to the production repository

   <CloudinaryImg
     mdx
     publicId='theodorusclarence/shorts/github/github-sync/github-sync-2'
     alt='github-sync-2'
     width={1636}
     height={1374}
   />

4. Copy the Personal Access Token and save it.

   <CloudinaryImg
     mdx
     publicId='theodorusclarence/shorts/github/github-sync/github-sync-3'
     alt='github-sync-3'
     width={1374}
     height={506}
   />

## Development Repository Setup

1. Save this workflow as `.github/workflows/git-sync.yml`

```yaml {9,17,19} title=".github/workflows/git-sync.yml" caption="Don't forget to change the username and repo name"
name: Git Sync
on:
  push:
    branches:
      - main
  workflow_dispatch:
jobs:
  git-sync:
    if: ${{ github.repository == 'username/dev-repo-name' }}
    runs-on: ubuntu-latest
    steps:
      - name: 🛑 Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.9.1
      - name: 🔗 Synchronize Repository
        uses: wei/git-sync@v3
        with:
          source_repo: 'https://username:${{ secrets.PAT }}@github.com/username/dev-repo-name.git'
          source_branch: 'main'
          destination_repo: 'https://username:${{ secrets.PAT }}@github.com/username/production-repo-name.git'
          destination_branch: 'main'
```

2. Add PAT to the GitHub secrets

   <CloudinaryImg
     mdx
     publicId='theodorusclarence/shorts/github/github-sync/github-sync-4'
     alt='github-sync-4'
     width={1952}
     height={1276}
   />

## Production Repository Setup

No setup is needed.
